package com.itic.appbase.applications.exports.ExportExcelWithZip;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itic.appbase.framework.utils.PropertyGetter;

/**
 * <p>Title: 导出工具类</p>
 * <p>Description: 中科软资料库</p>
 * <p>Copyright: Copyright (c) 2010-2013</p>
 * <p>Company: 中科软</p>
 * <p>@author 高晨晨</p>
 * <p>© 2004 sinosoft.com.cn</p>
 */
public class ExportExcelUtils {
	private static final Logger log = LoggerFactory.getLogger(ExportExcelUtils.class);
	
	public static void exportExcelUtil(String bzmc,List<?> objList,HttpServletRequest request, HttpServletResponse response) throws Exception{
		// 读取出导出excel的数据
		String id = PropertyGetter.getString(bzmc + ".id");
		String path = PropertyGetter.getString(bzmc + ".templatePath");
		String fieldsString = PropertyGetter.getString(bzmc + ".fields");
		String xy = PropertyGetter.getString(bzmc + ".xy");
		System.out.println("id:" + id + ",templatePath:" + path + ",fields:" + fieldsString + ",xy:" + xy);
		String[] fields = fieldsString.split(",");
		String absolutePath = request.getSession().getServletContext().getRealPath("/");
		String templatePath = absolutePath + "fileupload/" + path;
		int startRow = Integer.parseInt(xy.substring(0,xy.indexOf(",")));
		int startColumn = Integer.parseInt(xy.substring(xy.indexOf(",")+1,xy.length()));
		ServletOutputStream os = response.getOutputStream();
		// 设置下载文件的类型
		response.setContentType("application/vnd.ms-excel");
		// 随机生成下载的文件名
		Random random = new Random();
		String newpath = "file" + random.nextInt() + ".xls";
		response.setHeader("Content-Disposition", "attachment;filename=\"" + newpath + "\"");
		PoiUtils.exportExcelToClientByTemplet(fields, templatePath, startRow,startColumn, objList, os);
		os.flush();
		os.close();
	}
	
	
	
	/**
	 * 带文件压缩的导出
	 * @param bzmc   标志名称
	 * @param objList   数据list
	 * @param rootDir   临时文件的路径
	 * @param excelFileName  生成的excel文件的名字
	 * @param request   httpServletRequest
	 * @throws Exception 
	 */
	public static void exportExcelToZipUtil(String bzmc,List<?> objList,String rootDir,String excelFileName, HttpServletRequest request) throws Exception{
		// 读取出导出excel的数据
		log.info("导出excel到zip开始");
		String id = PropertyGetter.getString(bzmc + ".id");
		String path = PropertyGetter.getString(bzmc + ".templatePath");
		String fieldsString = PropertyGetter.getString(bzmc + ".fields");
		String xy = PropertyGetter.getString(bzmc + ".xy");
		boolean hasParent = PropertyGetter.getBoolean(bzmc + ".parent");
        log.info("id:" + id + ",templatePath:" + path + ",fields:" + fieldsString + ",xy:" + xy + ",parent:" + hasParent);
		String[] fields = fieldsString.split(",");
		String absolutePath = request.getSession().getServletContext().getRealPath("/");
		String templatePath = absolutePath + "static/template/excelExport/" + path;
		int startRow = Integer.parseInt(xy.substring(0,xy.indexOf(",")));
		int startColumn = Integer.parseInt(xy.substring(xy.indexOf(",")+1,xy.length()));
		String newpath = excelFileName;
		File filePath  = new File(rootDir);
		if (!filePath.exists()) {
			filePath.mkdirs();
		}
		BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(new File(filePath, newpath)));
		PoiUtils.exportExcelToClientByTemplet(fields, templatePath, startRow,startColumn, objList, os, hasParent);
		os.flush();
		os.close();
		log.info("导出excel到zip结束");
	}
}
